001 /*
002 * Copyright 1996-2005 Mort Bay Consulting Pty. Ltd.
003 * Copyright 2006 Stephen McConnell.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package net.dpml.test.http;
019
020 import java.io.IOException;
021 import java.io.PrintWriter;
022 import java.util.Date;
023 import java.util.Enumeration;
024
025 import javax.servlet.ServletConfig;
026 import javax.servlet.ServletException;
027 import javax.servlet.http.HttpServlet;
028 import javax.servlet.http.HttpServletRequest;
029 import javax.servlet.http.HttpServletResponse;
030 import javax.servlet.http.HttpSession;
031
032 /** Test Servlet Sessions.
033 *
034 * @author Greg Wilkins (gregw)
035 */
036 public class SessionDump extends HttpServlet
037 {
038
039 private int m_redirectCount = 0;
040
041 /**
042 * Servlet initialization.
043 * @param config the servlet configuration
044 * @exception ServletException if a configuration error occurs
045 */
046 public void init( ServletConfig config ) throws ServletException
047 {
048 super.init( config );
049 }
050
051 /**
052 * Process an incomming post request.
053 * @param request the http request
054 * @param response the http response
055 * @exception ServletException if a servlet processing error occurs
056 * @exception IOException if an IO error occurs
057 */
058 public void doPost( HttpServletRequest request, HttpServletResponse response )
059 throws ServletException, IOException
060 {
061 HttpSession session = request.getSession( false );
062 String action = request.getParameter( "Action" );
063 String name = request.getParameter( "Name" );
064 String value = request.getParameter( "Value" );
065
066 String nextUrl = getURI( request ) + "?R=" + m_redirectCount++;
067 if( action.equals( "New Session" ) )
068 {
069 session = request.getSession( true );
070 session.setAttribute( "test", "value" );
071 }
072 else if( session!=null )
073 {
074 if( action.equals( "Invalidate" ) )
075 {
076 session.invalidate();
077 }
078 else if( action.equals( "Set" ) && ( name != null ) && ( name.length() > 0 ) )
079 {
080 session.setAttribute( name, value );
081 }
082 else if( action.equals( "Remove" ) )
083 {
084 session.removeAttribute( name );
085 }
086 }
087
088 String encodedUrl = response.encodeRedirectURL( nextUrl );
089 response.sendRedirect( encodedUrl );
090 }
091
092 /**
093 * Process an incomming get request.
094 * @param request the http request
095 * @param response the http response
096 * @exception ServletException if a servlet processing error occurs
097 * @exception IOException if an IO error occurs
098 */
099 public void doGet( HttpServletRequest request, HttpServletResponse response )
100 throws ServletException, IOException
101 {
102 response.setContentType( "text/html" );
103
104 HttpSession session = request.getSession( getURI( request ).indexOf( "new" ) > 0 );
105
106 PrintWriter out = response.getWriter();
107 out.println( "<h1>Session Dump Servlet:</h1>" );
108 out.println( "<form action=\"" + response.encodeURL( getURI( request ) ) + "\" method=\"post\">" );
109
110 if( session == null )
111 {
112 out.println( "<H3>No Session</H3>" );
113 out.println( "<input type=\"submit\" name=\"Action\" value=\"New Session\"/>" );
114 }
115 else
116 {
117 try
118 {
119 out.println( "<b>ID:</b> " + session.getId() + "<br/>" );
120 out.println( "<b>New:</b> " + session.isNew() + "<br/>" );
121 out.println( "<b>Created:</b> " + new Date( session.getCreationTime() ) + "<br/>" );
122 out.println( "<b>Last:</b> " + new Date( session.getLastAccessedTime() ) + "<br/>" );
123 out.println( "<b>Max Inactive:</b> " + session.getMaxInactiveInterval() + "<br/>" );
124 out.println( "<b>Context:</b> " + session.getServletContext() + "<br/>" );
125
126 Enumeration keys = session.getAttributeNames();
127 while( keys.hasMoreElements() )
128 {
129 String name = (String) keys.nextElement();
130 String value = (String) session.getAttribute( name );
131 out.println( "<b>" + name + ":</b> " + value + "<br/>" );
132 }
133
134 out.println( "<b>Name:</b><input type=\"text\" name=\"Name\" /><br/>" );
135 out.println( "<b>Value:</b><input type=\"text\" name=\"Value\" /><br/>" );
136
137 out.println( "<input type=\"submit\" name=\"Action\" value=\"Set\"/>" );
138 out.println( "<input type=\"submit\" name=\"Action\" value=\"Remove\"/>" );
139 out.println( "<input type=\"submit\" name=\"Action\" value=\"Invalidate\"/><br/>" );
140
141 out.println( "</form><br/>" );
142
143 if( request.isRequestedSessionIdFromCookie() )
144 {
145 out.println( "<P>Turn off cookies in your browser to try url encoding<BR>" );
146 }
147 if( request.isRequestedSessionIdFromURL() )
148 {
149 out.println( "<P>Turn on cookies in your browser to try cookie encoding<BR>" );
150 }
151 }
152 catch( IllegalStateException e )
153 {
154 e.printStackTrace();
155 }
156 }
157 }
158
159 /**
160 * Return the servlet info.
161 * @return the info
162 */
163 public String getServletInfo()
164 {
165 return "Session Dump Servlet";
166 }
167
168 private String getURI( HttpServletRequest request )
169 {
170 String uri = (String) request.getAttribute( "javax.servlet.forward.request_uri" );
171 if( uri == null )
172 {
173 uri = request.getRequestURI();
174 }
175 return uri;
176 }
177 }